/*
 * Copyright (C) 2012 Jonathan St-Michel <jonathan@abiwebz.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.abiwebz.platform.util.job;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.JProgressBar;

/**
 *
 * @author stmicj01
 */
public class JobManager implements PropertyChangeListener {
    /********* SINGLETON *****************************************************/
    protected static JobManager singletonInstance = null;

    /**
     * Retourner une copie de l'application pour récupérer les informations
     * répertoriées au démarrage de celle-ci.
     * 
     * @return Une copie de l'application
     */
    public static JobManager getInstance() {
        return JobManager.singletonInstance;
    }

    /**
     * Créer selon la technique du "singleton" l'instance de la classe 
     * Application qui pourra être récupérée de n'importe où.
     * 
     * @return Une copie de la nouvelle application
     */
    public static JobManager newInstance(JProgressBar progressbar) {
        JobManager.singletonInstance = new JobManager(progressbar);
        return JobManager.getInstance();
    }
    

    /*************************************************************************/
    
    private JProgressBar progressbar;
    private ArrayList<Job> jobs;
    private Job current;
    
    public JobManager(JProgressBar progressbar) {
        this.progressbar = progressbar;
        this.progressbar.setStringPainted(true);
        this.progressbar.setString(" "); //Cacher le texte
        this.jobs = new ArrayList<Job>();
        this.current = null;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            progressbar.setValue(progress);
        } 
    }
    
    public void queue(Job job){
        job.addPropertyChangeListener(this);
        this.jobs.add(job);
        next();
    }
    
    public void next(){
        if(this.current == null){
            if(this.jobs.size() > 0){
                this.current = this.jobs.get(0);
                this.progressbar.setToolTipText("Tâche en cours : " + this.current.getJobName());
                progressbar.setIndeterminate(this.current.getMaximum() == 0);
                this.progressbar.setString(progressbar.isIndeterminate() ? " " : null);
                this.current.execute();
            }
            else{
                this.progressbar.setToolTipText("Aucune tâche en cours");
            }
        }
    }
    
    public void finish(){
        if(this.current != null){
            this.jobs.remove(this.current);
            this.current = null;
            this.progressbar.setString(" ");
            next();
        }
    }
    
}
